package com.example.graduation.controller;

import com.example.graduation.entity.InterviewInvitation;
import com.example.graduation.service.InterviewInvitationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/interview-invitations")
public class InterviewInvitationController {
    @Autowired
    private InterviewInvitationService interviewInvitationService;

    @GetMapping
    public ResponseEntity<List<InterviewInvitation>> getAllInvitations() {
        List<InterviewInvitation> invitations = interviewInvitationService.findAll();
        return ResponseEntity.ok(invitations);
    }

    @GetMapping("/{id}")
    public ResponseEntity<InterviewInvitation> getInvitationById(@PathVariable Integer id) {
        InterviewInvitation invitation = interviewInvitationService.findById(id);
        if (invitation != null) {
            return ResponseEntity.ok(invitation);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/job-seeker/{jobSeekerId}")
    public ResponseEntity<List<InterviewInvitation>> getInvitationsByJobSeeker(@PathVariable Integer jobSeekerId) {
        List<InterviewInvitation> invitations = interviewInvitationService.findByJobSeekerId(jobSeekerId);
        return ResponseEntity.ok(invitations);
    }

    @GetMapping("/company-job/{companyJobId}")
    public ResponseEntity<List<InterviewInvitation>> getInvitationsByCompanyJob(@PathVariable Integer companyJobId) {
        List<InterviewInvitation> invitations = interviewInvitationService.findByCompanyJobId(companyJobId);
        return ResponseEntity.ok(invitations);
    }

    @PostMapping
    public ResponseEntity<InterviewInvitation> createInvitation(@RequestBody InterviewInvitation interviewInvitation) {
        int result = interviewInvitationService.insert(interviewInvitation);
        if (result > 0) {
            return ResponseEntity.ok(interviewInvitation);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<InterviewInvitation> updateInvitation(@PathVariable Integer id, @RequestBody InterviewInvitation interviewInvitation) {
        interviewInvitation.setId(id);
        int result = interviewInvitationService.update(interviewInvitation);
        if (result > 0) {
            return ResponseEntity.ok(interviewInvitation);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteInvitation(@PathVariable Integer id) {
        int result = interviewInvitationService.deleteById(id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 